package com.example.zp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private WechatFragment wechatFragment = new WechatFragment();
    private Fragment friendFragment = new FriendFragment();
    private Fragment addressFragment = new AddressFragment();
    private Fragment settingFragment = new SettingFragment();

    private FragmentManager fm;

    private LinearLayout wechatTab;
    private LinearLayout friendTab;
    private LinearLayout addressTab;
    private LinearLayout settingTab;

    private ImageButton wechatImg;
    private ImageButton friendImg;
    private ImageButton addressImg;
    private ImageButton settingImg;

    private RecyclerView recyclerView;
    private List<String> list;
    private Context context;
    private Adapter adapter;

    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;
    private LocationClient mLocationClient = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragment();
        initView();
        initEvent();
        // 初始化时位置在首页
        selectFragment(0);
    }

    /**
     * 初始化FragmentManager，并放入页面
     */
    private void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        // 将四个页面放入 fragmentLayout 中
        transaction.add(R.id.id_content, wechatFragment);
        transaction.add(R.id.id_content, friendFragment);
        transaction.add(R.id.id_content, addressFragment);
        transaction.add(R.id.id_content, settingFragment);
        transaction.commit();
    }

    /**
     * 初始化响应区
     */
    private void initView() {
        // 获取Layout和ImageButton
        wechatTab = findViewById(R.id.tab_wechat);
        friendTab = findViewById(R.id.tab_friend);
        addressTab = findViewById(R.id.tab_address);
        settingTab = findViewById(R.id.tab_setting);

        wechatImg = findViewById(R.id.img_wechat);
        friendImg = findViewById(R.id.img_friend);
        addressImg = findViewById(R.id.img_address);
        settingImg = findViewById(R.id.img_setting);

//
//        View addressView = getLayoutInflater().inflate(R.layout.tab_address, null, false);
//        mMapView = addressView.findViewById(R.id.bmapView);
////        mMapView =  findViewById(R.id.bmapView);
//        mBaiduMap = mMapView.getMap();
//        mBaiduMap.setMyLocationEnabled(true);
//        // 开启定位图层
//        mBaiduMap.setMyLocationEnabled(true);
    }

    /**
     * 初始化点击事件，将监听过程限制在 四个LinearLayout
     */
    private void initEvent() {
        wechatTab.setOnClickListener(this);
        friendTab.setOnClickListener(this);
        addressTab.setOnClickListener(this);
        settingTab.setOnClickListener(this);
    }

    /**
     * 切换tab 的函数，包含隐藏和高亮
     * @param i
     */
    private void selectFragment(int i) {
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        // 高亮显示
        switch (i) {
            case 0:
                transaction.show(wechatFragment);
//                initWechat();
                wechatImg.setImageResource(R.drawable.wechat_press);
                break;
            case 1:
                transaction.show(friendFragment);
                friendImg.setImageResource(R.drawable.friend_press);
                break;
            case 2:
                transaction.show(addressFragment);
                addressImg.setImageResource(R.drawable.address_press);
                break;
            case 3:
                transaction.show(settingFragment);
                settingImg.setImageResource(R.drawable.setting_press);
            default:
                break;
        }
        transaction.commit();
    }

    /**
     * 将之前显示的 Fragment 隐藏
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(wechatFragment);
        transaction.hide(friendFragment);
        transaction.hide(addressFragment);
        transaction.hide(settingFragment);
    }

    /**
     * 复原下方图标
     */
    private void resetImgs() {
        wechatImg.setImageResource(R.drawable.wechat);
        friendImg.setImageResource(R.drawable.friend);
        addressImg.setImageResource(R.drawable.address);
        settingImg.setImageResource(R.drawable.setting);
    }

    @Override
    public void onClick(View v) {

        resetImgs();
        switch (v.getId()) {
            case R.id.tab_wechat:
                selectFragment(0);
                break;
            case R.id.tab_friend:
                selectFragment(1);
                break;
            case R.id.tab_address:
                selectFragment(2);
                break;
            case R.id.tab_setting:
                selectFragment(3);
                break;
            default:
                break;
        }
    }

    // 弃用
    private void setLocation() {
        SDKInitializer.initialize(this.getApplicationContext());//初始化操作，传入Context
        //定位初始化
        mLocationClient = new LocationClient(getApplicationContext());

        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);

        //开启地图定位图层
        mLocationClient.start();
    }


    // 获取定位数据的监听器
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    // 请求权限 已弃用
    protected void judgePermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝

            // sd卡权限
            String[] SdCardPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, SdCardPermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, SdCardPermission, 100);
            }

            //手机状态权限
            String[] readPhoneStatePermission = {Manifest.permission.READ_PHONE_STATE};
            if (ContextCompat.checkSelfPermission(this, readPhoneStatePermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, readPhoneStatePermission, 200);
            }

            //定位权限
            String[] locationPermission = {Manifest.permission.ACCESS_FINE_LOCATION};
            if (ContextCompat.checkSelfPermission(this, locationPermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, locationPermission, 300);
            }

            String[] ACCESS_COARSE_LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION};
            if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, ACCESS_COARSE_LOCATION, 400);
            }


            String[] READ_EXTERNAL_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, READ_EXTERNAL_STORAGE, 500);
            }

            String[] WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, WRITE_EXTERNAL_STORAGE, 600);
            }

        }else{
            //doSdCardResult();
        }
        //LocationClient.reStart();
    }

}
